using System;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.ComponentModel.Design.Serialization;
using System.Data;
using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Wicresoft.IDE.Loader;

namespace Wicresoft.IDE.Designer
{
	public enum LoaderType
	{
        UMCDesignerLoader = 1
	}

    /// <summary>
    /// Manages numerous HostSurfaces. Any services added to HostSurfaceManager
    /// will be accessible to all HostSurfaces
    /// </summary>
	public class HostSurfaceManager : DesignSurfaceManager
	{

		public HostSurfaceManager() : base()
		{
			this.AddService(typeof(INameCreationService), new NameCreationService());
            this.ActiveDesignSurfaceChanged += new ActiveDesignSurfaceChangedEventHandler(HostSurfaceManager_ActiveDesignSurfaceChanged);
		}

		protected override DesignSurface CreateDesignSurfaceCore(IServiceProvider parentProvider)
		{
			return new HostSurface(parentProvider);
		}

        /// <summary>
        /// Gets a new HostSurface and loads it with the appropriate type of
        /// root component. 
        /// </summary>
        private HostControl GetNewHost(Type rootComponentType)
		{
			HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);

            if (rootComponentType == typeof(UmcProvider.UmcHost))
                hostSurface.BeginLoad(typeof(UmcProvider.UmcHost));
            else
				throw new Exception("Undefined Wicresoft.IDE.Designer Type: " + rootComponentType.ToString());

			hostSurface.Initialize();
			this.ActiveDesignSurface = hostSurface;
			return new HostControl(hostSurface);
		}

        /// <summary>
        /// Gets a new HostSurface and loads it with the appropriate type of
        /// root component. Uses the appropriate Loader to load the HostSurface.
        /// </summary>
		public HostControl GetNewHost(Type rootComponentType, LoaderType loaderType)
		{
            //if (loaderType == LoaderType.NoLoader)
            //    return GetNewHost(rootComponentType);

			HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
			IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost));

			switch (loaderType)
			{
				case LoaderType.UMCDesignerLoader:
                    UMCHostLoader umcLoader = new UMCHostLoader(rootComponentType);
                    hostSurface.BeginLoad(umcLoader);
                    hostSurface.Loader = umcLoader;
                    break;
				default:
					throw new Exception("Loader is not defined: " + loaderType.ToString());
			}

			hostSurface.Initialize();
			return new HostControl(hostSurface);
		}

        /// <summary>
        /// Opens an Xml file and loads it up using BasicHostLoader (inherits from
        /// BasicDesignerLoader)
        /// </summary>
		public HostControl GetNewHost(string fileName)
		{
            if (fileName == null || !File.Exists(fileName))
            {
                MessageBox.Show("FileName is incorrect: " + fileName);
            }

            LoaderType loaderType = LoaderType.UMCDesignerLoader;

			HostSurface hostSurface = (HostSurface)this.CreateDesignSurface(this.ServiceContainer);
			IDesignerHost host = (IDesignerHost)hostSurface.GetService(typeof(IDesignerHost));

            if (loaderType == LoaderType.UMCDesignerLoader)
            {
                UMCHostLoader umcLoader = new UMCHostLoader(fileName);
                hostSurface.BeginLoad(umcLoader);
                hostSurface.Loader = umcLoader;
                hostSurface.Initialize();
            }
			return new HostControl(hostSurface);
		}

		public void AddService(Type type, object serviceInstance)
		{
			this.ServiceContainer.AddService(type, serviceInstance);
		}

        /// <summary>
        /// Uses the OutputWindow service and writes out to it.
        /// </summary>
		void HostSurfaceManager_ActiveDesignSurfaceChanged(object sender, ActiveDesignSurfaceChangedEventArgs e)
		{
            
		}
	}// class
}// namespace
